package com.wwy.arithmetic.suanfa;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 根据给出的Excel序列号反向查出数字
 */
public class ReturnExcelNum {
    public static void main(String[] args) {
        List<Character> letters = IntStream.rangeClosed('A', 'Z')
                .mapToObj(c -> (char) c)
                .collect(Collectors.toList());
        int zy = titleToNumber("FXSHRXW");
        System.out.println("FXSHRXW = " + zy);
    }

    static int getReturnNumber(String excelName, List<Character> list) {
        char[] chars = excelName.toCharArray();
        int num = 0;
        for (int i = chars.length - 1; i >= 0; i--) {
            if (i == chars.length - 1) {
                int i1 = list.indexOf(chars[i]);
                num = num+i1+1;
            }else{
                double pow = Math.pow(26, (double) chars.length - i-1);
                num = (int) (num+((list.indexOf(chars[i])+1)*pow));
            }
        }
        return num;
    }


    public static int titleToNumber(String columnTitle) {
        int number = 0;
        int multiple = 1;
        for (int i = columnTitle.length() - 1; i >= 0; i--) {
            int k = columnTitle.charAt(i) - 'A' + 1;
            number += k * multiple;
            multiple *= 26;
        }
        return number;
    }

}
